package com.teichmann.engine;

import java.io.IOException;
import java.util.List;

import com.teichmann.bo.Car;
import com.teichmann.core.OnlinePage;
import com.teichmann.core.targets.online.AutoCarro;
import com.teichmann.core.targets.online.AutoCentro;
import com.teichmann.core.targets.online.CarroNaSerra;
import com.teichmann.core.targets.online.MeuCarroNovo;
import com.teichmann.core.targets.online.SulRevendas;

public class OnlineExecutor {

	private OnlinePage[] pages = new OnlinePage[0];
	private String[] sources = new String[0];
	private String parameter = null;

	public OnlineExecutor(String parameter, String[] sources) {
		this.parameter = parameter;
		this.sources = sources;
	}

	public OnlineExecutor(String parameter) {
		this.parameter = parameter;
	}

	public List<Car> execute() throws IOException {
		try {
			CarSearch search = new CarSearch(buildSources(), true);
			return search.process(parameter);
		} catch (IOException e) {
			throw e;
		}
	}

	private OnlinePage[] buildSources() {
		if (this.sources != null) {
			pages = new OnlinePage[sources.length];
			int i = 0;
			for (String s : sources) {
				if (s.equals(AutoCarro.class.getSimpleName()))
					pages[i++] = new com.teichmann.core.targets.online.AutoCarro();
				else if (s.equals(MeuCarroNovo.class.getSimpleName()))
					pages[i++] = new com.teichmann.core.targets.online.MeuCarroNovo();
				else if (s.equals(AutoCentro.class.getSimpleName()))
					pages[i++] = new com.teichmann.core.targets.online.AutoCentro();
				else if (s.equals(SulRevendas.class.getSimpleName()))
					pages[i++] = new com.teichmann.core.targets.online.SulRevendas();
				else if (s.equals(CarroNaSerra.class.getSimpleName()))
					pages[i++] = new com.teichmann.core.targets.online.CarroNaSerra();

			}
		}
		return pages;
	}

	public static void main(String[] args) throws IOException {
		new OnlineExecutor("tempra").execute();
	}
}
